﻿using Common.Type;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Messaging;
using System.Text;
using System.Threading.Tasks;

namespace Common.Service.Queue
{
    public static class QueueFactory
    {
        private static MessageQueue messageQueue;
        public static MessageQueue Queue
        {
            get
            {
                InitializeQueue();
                return messageQueue;
            }
        }


        private static void InitializeQueue()
        {
            string queuePath = QueueConstant.QueueName;
            if (!MessageQueue.Exists(queuePath))
            {
                messageQueue = MessageQueue.Create(queuePath);
                //Local System Account
                messageQueue.SetPermissions(QueueConstant.LOCAL_SYSTEM_ACCOUNT, MessageQueueAccessRights.FullControl);

                //Everyone User Accout
                messageQueue.SetPermissions(QueueConstant.EVERYONE_ACCOUNT, MessageQueueAccessRights.WriteMessage);
            }
            else
            {
                messageQueue = new MessageQueue(queuePath);
            }
            messageQueue.Formatter = new BinaryMessageFormatter();
        }
    }
}
